

package com.kehutong.house.service;
 import com.kehutong.house.entity.Community;
        import com.kehutong.house.entity.Shop;
        import com.kehutong.house.entity.ShopClassify;
        import org.coraframework.inject.Inject;
        import org.coraframework.json.JSONObject;
        import org.coraframework.mvc.http.Bean;
        import org.coraframework.mvc.http.ReqMapping;
        import org.coraframework.mvc.http.result.Result;
        import org.coraframework.mvc.webservice.WebService;
        import org.coraframework.orm.Pool;
        import org.coraframework.orm.jdbc.JdbcSession;
        import org.coraframework.orm.jdbc.Page;
        import org.coraframework.orm.jdbc.execute.entity.FindOne;
        import org.coraframework.orm.jdbc.execute.entity.FindPage;
        import org.coraframework.util.Objects;

        import java.util.List;

@WebService("/basic/shop/classify/service/api")
public class ShopClassifyServiceApi {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private CommunityService communityService;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        FindPage<ShopClassify> find = jdbcSession.findPage(ShopClassify.class)
                .in(jsonObject, "id")
                .eq(jsonObject, "name").and()
                .eq("deleted", false);
        Page<ShopClassify> page = find.page(jsonObject).exe();

        return page;
    }

    @ReqMapping("/get")
    public Object get(ShopClassify item) {
        return item;
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(ShopClassify shopClassify) {
        FindOne<ShopClassify> find = jdbcSession.findOne(ShopClassify.class)
                .eq("deleted", false)
                .eq("communityNo", shopClassify.getCommunityNo())
                .eq("name", shopClassify.getName());

        ShopClassify item = find.exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "当前小区下已有相同的楼栋信息，请修改后重试");
        }

        Community com = Pool.get(Community.class, shopClassify.getCommunityNo());
        shopClassify.setDepartmentNo(com.getParent_id());
        shopClassify.setDepartmentNos(com.getParent_ids());

        jdbcSession.insert(shopClassify);
        return Result.success(shopClassify);
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(ShopClassify shopClassify) throws Exception {

        FindOne<ShopClassify> find = jdbcSession.findOne(ShopClassify.class)
                .eq("deleted", false)
                .eq("communityNo", shopClassify.getCommunityNo())
                .eq("name", shopClassify.getName())
                .noEq("id", shopClassify.getId());
        ShopClassify item = find.exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "当前小区下已有相同的商铺分类信息，请修改后重试");
        }

        List<Shop> shops = jdbcSession.findArray(Shop.class)
                .eq("communityNo", shopClassify.getCommunityNo())
                .eq("shopClassifyNo",shopClassify.getId())
                .exe();

        shops.forEach(house -> {
            house.setShopClassifyName(shopClassify.getName());
        });
        jdbcSession.updateById(shops);

        Community com = Pool.get(Community.class, shopClassify.getCommunityNo());
        shopClassify.setDepartmentNo(com.getParent_id());
        shopClassify.setDepartmentNos(com.getParent_ids());

        jdbcSession.updateById(shopClassify);

        return Result.success(shopClassify);
    }

    @ReqMapping("/delete")
    public Object delete(ShopClassify item) {
        List<Shop> shops = jdbcSession.findArray(Shop.class)
                .eq("communityNo", item.getCommunityNo())
                .eq("shopClassifyNo", item.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonEmpty(shops)){
            return Result.error(0, "需先删除该商铺分类下的商铺，再删除该商铺分类！");
        }


        item.setDeleted(true);
        jdbcSession.updateById(item);

        return Result.success();
    }

}
